﻿using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using FFIV.Lib.DataComponent;
using FFIV.Lib.IView.Articles;
using FFIV.Lib.Utilities;

public partial class Views_Articles_ArticlesList : UserControl, IArticlesList
{
	private PagedDataSource m_PagedDataSource;
	private int m_PageIndex;

	public int PageSize = 5;

	public AritcleDS.ArticlesDetailDataTable Articles
	{
		set
		{
			InitData(value);
		}
	}

	public string NextPageUrl
	{
		set { lbl_NextPage.HRef = value; }
	}

	public string PreviousPageUrl
	{
		set { lbl_PrePage.HRef = value; }
	}

	public int PageIndex
	{
		get { return m_PageIndex; }
	}

	public void RegistEvents()
	{
		dl_Articles.ItemDataBound += dl_Articles_ItemDataBound;
	}

	public string MainTitle
	{
		set { lit_MainTitle.Text = value; }
	}

	public string SubTitle
	{
		set { lit_SubTitle.Text = value; }
	}

	private void InitData(DataTable dataTable)
	{
		DataView dataView = dataTable.DefaultView;
		m_PagedDataSource = new PagedDataSource();
		m_PagedDataSource.DataSource = dataView;

		m_PagedDataSource.AllowPaging = true;
		m_PagedDataSource.PageSize = PageSize;

		m_PageIndex = ParamUtility.GetQueryStringValue("page", 0);
		m_PagedDataSource.CurrentPageIndex = m_PageIndex;

		if (m_PagedDataSource.IsFirstPage)
		{
			lbl_PrePage.Visible = false;
		}
		if (m_PagedDataSource.IsLastPage)
		{
			lbl_NextPage.Visible = false;
		}

		dl_Articles.DataSource = m_PagedDataSource;
		dl_Articles.DataBind();
		ErrorInfo.Visible = false;

		if (dataTable.Rows.Count <= 0)
		{
			ErrorInfo.Visible = true;
			lbl_ErrorInfo.Text = "抱歉，没有符合要求的文章";
		}
	}

	private static void dl_Articles_ItemDataBound(object sender, DataListItemEventArgs e)
	{
		DataRowView item = e.Item.DataItem as DataRowView;
		if (item != null)
		{
			HtmlGenericControl artilecId = e.Item.FindControl("lbl_ArticleId") as HtmlGenericControl;
			if (artilecId != null)
			{
				artilecId.InnerText = item["ArticleId"].ToString();
			}

			HtmlAnchor title = e.Item.FindControl("lkb_Title") as HtmlAnchor;
			if (title != null)
			{
				title.InnerText = HttpUtility.HtmlDecode(item["Title"].ToString());
				title.HRef = UrlUtility.GetArticleDetailUrl(Convert.ToInt32(item["ArticleId"]));
			}

			HtmlGenericControl summary = e.Item.FindControl("lbl_Summary") as HtmlGenericControl;
			if (summary != null)
			{
				summary.InnerText = HttpUtility.HtmlDecode(item["Summary"].ToString());
			}

			HtmlGenericControl author = e.Item.FindControl("lbl_Author") as HtmlGenericControl;
			if (author != null)
			{
				author.InnerText = item["AuthorName"].ToString();
			}

			HtmlGenericControl lastUpdateDate = e.Item.FindControl("lbl_LastUpdateDate") as HtmlGenericControl;
			if (lastUpdateDate != null)
			{
				if (item["LastUpdateDate"] is DateTime)
				{
					string nowTime = ((DateTime)item["LastUpdateDate"]).ToString("yyyy-MM-dd");
					//DateTime dt = DateTime.ParseExact(nowTime, "yy-MM-dd", null);
					lastUpdateDate.InnerText = nowTime;
				}
			}

			HtmlAnchor category = e.Item.FindControl("lbl_Category") as HtmlAnchor;
			if (category != null)
			{
				category.InnerText = item["CategoryName"].ToString();
				category.HRef = UrlUtility.GetCategoriesUrl(Convert.ToInt32(item["CategoryId"]));
			}
		}
	}
}
